<template>
  <div class="app-container clearfix">
    <!-- 搜索 -->
    <salary-search
      :from="from"
      :init-params="initParams"
      :search-params="searchKey"
      :department-all-list-data="departmentAllListData"
      @on-init="handleInitList"
    />
    <!-- 列表 -->
    <salary-table
      :from="from"
      :data="listData"
      :search-params="searchKey"
      :total-count="totalCount"
      :list-loading="listLoading"
      @on-init="handleInitList"
      @on-edit="handleEditShow"
      @on-back="handleBackShow"
      @on-view="handleViewShow"
      @on-print="handlePrint"
      @on-select="handleSelect"
    />
    <!-- 编辑弹出框 -->
    <salary-make
      :visible.sync="editInfo.visible"
      :department-all-list-data="departmentAllListData"
      @on-created="handleCreated"
    />
    <!-- 撤回审批弹出框 -->
    <salary-back
      :visible.sync="backInfo.visible"
      :detail-data="backInfo.detailInfo"
      @on-init="handleInitList"
    />

    <!-- 开票详情弹出框 -->
    <piao-data
      :visible.sync="piaodata.visible"
      :detail-data="piaodata.detailInfo"
      @on-export="handleExport"
      @on-init="handleInitList"
    />


  </div>
</template>

<script>
import { getSalaryListAPI } from '@/api/todo/salary'
import { SalarySearch, SalaryTable, SalaryMake, SalaryBack, PiaoData } from './components'
import treeAllListData from '@/mixins/treeAllListData'
import {
  getPiaoListSummaryAPI,
  getPiaoListUpdateAPI,
  savePiaomakeAPI,
  submitApproveAPI,
  deleteApproveAPI,
  getInvoiceTreeAPI,
  drawbackAPI
} from '@/api/todo/piaomake'
import request from '@/utils/request'

export default {
  name: 'piaolist',
  components: {
    SalarySearch,
    SalaryTable,
    SalaryMake,
    SalaryBack,
    PiaoData
  },
  mixins: [treeAllListData],
  props: {
    from: {
                type: String,
                required: false,
                default: ''
            }
  },
  data() {
    return {
      piaodata: {
        visible: false,
        detailInfo: {}
      },
      initParams: { // 初始化参数
        pageNum: 1, // 当前页数
        pageSize: 10, // 每页个数
        // sort: '', // 排序

        projectName: '',
        projectNo: '',
        contractNo: '',
        expenseMonth: ''

      },
      searchKey: {}, // 搜索参数
      editInfo: { // 编辑弹出框
        visible: false, // 是否显示模态框
        detailInfo: {}
      },
      backInfo: { // 撤回弹出框
        visible: false, // 是否显示模态框
        detailInfo: {}
      },
      totalCount: 0, // 分页用的列表总条数
      listLoading: false,
      listData: [] // 列表数据
    }
  },
  computed: {},
  watch: {
    initParams: {
      handler: function(newVal, oldVal) {
        if (JSON.stringify(newVal) !== JSON.stringify(this.searchKey)) {
          this.searchKey = JSON.parse(JSON.stringify(newVal))
        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {
  },
  mounted() {
    this.initData()
  },
  methods: {
    	goSalaryreissue({ type, data }) { // 进入工资补发
      let query = { type }
      if (type === '1') {
        query = {
          ...query,
          ...data
        }
      } else {
        query['id'] = data.id
      }
      // console.log(query)
      this.$router.push({
        name: 'Salaryreissue',
        query
      })
    },
    goPiaomak({ type, data }) { // 进入开票制作
      let query = { type }
      if (type == '1') {
        query = {
          ...query,
          ...data
        }
      } else {
        query['id'] = data.id
        query['source']=data.source
      }
      // console.log(query)
      this.$router.push({
        name: 'Piaomake',
        query
      })
    },
    handleEditShow(data = {}) { // 编辑－显示弹出框
      if (!data) {
        this.goPiaomak({
          type: '1',
          data
        })
      } else {
        this.goPiaomak({
          type: '2',
          data
        })
      }
    },
    handleBackShow(data = {}) { // 撤回审批－显示弹出框
      this.backInfo.visible = true
      this.backInfo.detailInfo = data
    },
    async handleViewShow(data = {}) {
      
      if (data) {
        data.source = 'piaolist'
        this.goPiaomak({
          type: '3',
          data
        })
      }
    },
    async handlePrint(data = {}) {
      this.piaodata.visible = true

      // 获取数据
      let response
      response = await getPiaoListUpdateAPI({
        id: data.id
      })

      console.log(response)
      this.piaodata.detailInfo = response.data
    },
    handleCreated(data = {}) { // 添加
      	console.log('页面跳转之前的参数为：', data)
      	// 跳工资补发页面
      	if (data.salaryType == 2) {
      		this.goSalaryreissue({
      			 type: '1',
          		data
      		})
      		return
      	}

      	// 跳月薪制作页面
      this.goPiaomak({
        type: '1',
        data
      })
    },
    handleUpdated() { // 编辑成功
      this.getListData()
    },
    handleInitList(params = {}) { // 初始化列表
      this.searchKey = {
        ...this.searchKey,
        ...params

      }

      this.getListData()
    },
    async getDepartmentAllListData() { // 获取所有数据
      try {
        await this.$store.dispatch('department/GetDepartmentList', {
          request: true,
          showChargedOnly: true
        })
      } catch (error) {
        // console.log(error)
        this.$notify.error({
          title: '错误',
          message: error.message
        })
      }
    },
    async getListData() { // 获取列表数据
      try {
        let { searchKey } = this
        searchKey = JSON.parse(JSON.stringify(searchKey))
        this.listLoading = true
        if (searchKey.kprq) {
          searchKey.beginTime = this.searchKey.kprq[0]
          searchKey.endTime = this.searchKey.kprq[1]
          delete searchKey.kprq
        }
        
        if(this.from && this.from == 'tuipiao'){
          searchKey.invoiceStatus = '已开票'
          //如果是退票则查询新的接口
          let that = this;
          request({
              url: '/hr/invoice/refund/apply/invoiceList',
              method: 'get',
              params: searchKey
          }).then(response => {
             let result = response
              if (+result.code === 200) {
                this.listData = result.rows || []
                this.totalCount = result.total || 0
              } else {
                this.$notify.error({
                  title: '错误',
                  message: result.msg || '获取列表失败！'
                })
              }
          })
          return

        }

        const response = await getPiaoListSummaryAPI({
          ...searchKey
        })
        const result = response
        if (+result.code === 200) {
          this.listData = result.rows || []
          this.totalCount = result.total || 0
        } else {
          this.$notify.error({
            title: '错误',
            message: result.msg || '获取列表失败！'
          })
        }
      } catch (error) {
        // console.log(error)
        this.$notify.error({
          title: '错误',
          message: error.message || '获取列表失败!'
        })
      } finally {
        this.listLoading = false
      }
    },

    async initData() { // 初始化数据
      this.getListData()
      this.getDepartmentAllListData()
    },
    handleSelect(data){
      this.$emit('on-select',data)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
